<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/star8kr/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/star8kr/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/star8kr/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/star8kr/images/logo.svg" color="#222">

<link rel="stylesheet" href="/star8kr/css/main.css">


<link rel="stylesheet" href="/star8kr/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/star8kr/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="一、stata 基础设置1.系统文件位置查询与修改123456789101112131415*-(1)查询系统文件夹储存位置. sysdir    STATA:  D:\stata16\    BASE:  D:\stata16\ado\base\    SITE:  D:\stata16\ado\site\    PLUS:  D:\stata16&#x2F;ado\plus\PERSONAL:  D:\">
<meta property="og:type" content="article">
<meta property="og:title" content="Stata 学习">
<meta property="og:url" content="https://gitee.com/star8kr/star8kr/2023/04/18/Stata%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="Zhang&#39;s blog">
<meta property="og:description" content="一、stata 基础设置1.系统文件位置查询与修改123456789101112131415*-(1)查询系统文件夹储存位置. sysdir    STATA:  D:\stata16\    BASE:  D:\stata16\ado\base\    SITE:  D:\stata16\ado\site\    PLUS:  D:\stata16&#x2F;ado\plus\PERSONAL:  D:\">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-04-18T03:17:07.353Z">
<meta property="article:modified_time" content="2023-05-13T12:36:40.082Z">
<meta property="article:author" content="zhang jiaxing">
<meta property="article:tag" content="Stata">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://gitee.com/star8kr/star8kr/2023/04/18/Stata%E5%AD%A6%E4%B9%A0/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Stata 学习 | Zhang's blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/star8kr/atom.xml" title="Zhang's blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/star8kr/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Zhang's blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">横看成岭侧成峰</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/star8kr/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/star8kr/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/star8kr/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/star8kr/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/star8kr/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/star8kr/star8kr/2023/04/18/Stata%E5%AD%A6%E4%B9%A0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/star8kr/images/avatar.gif">
      <meta itemprop="name" content="zhang jiaxing">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Zhang's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Stata 学习
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2023-04-18 11:17:07" itemprop="dateCreated datePublished" datetime="2023-04-18T11:17:07+08:00">2023-04-18</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-05-13 20:36:40" itemprop="dateModified" datetime="2023-05-13T20:36:40+08:00">2023-05-13</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/star8kr/categories/Stata/" itemprop="url" rel="index"><span itemprop="name">Stata</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.3k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>11 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="一、stata-基础设置"><a href="#一、stata-基础设置" class="headerlink" title="一、stata 基础设置"></a>一、stata 基础设置</h2><h3 id="1-系统文件位置查询与修改"><a href="#1-系统文件位置查询与修改" class="headerlink" title="1.系统文件位置查询与修改"></a>1.系统文件位置查询与修改</h3><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">*-(1)查询系统文件夹储存位置</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">sysdir</span> </span><br><span class="line">   STATA:  <span class="keyword">D</span>:\stata16\</span><br><span class="line">    BASE:  <span class="keyword">D</span>:\stata16\<span class="keyword">ado</span>\base\</span><br><span class="line">    SITE:  <span class="keyword">D</span>:\stata16\<span class="keyword">ado</span>\site\</span><br><span class="line">    PLUS:  <span class="keyword">D</span>:\stata16/<span class="keyword">ado</span>\plus\</span><br><span class="line"><span class="keyword">PERSONAL</span>:  <span class="keyword">D</span>:\stata16/<span class="keyword">ado</span>\<span class="keyword">personal</span>\</span><br><span class="line">OLDPLACE:  c:\<span class="keyword">ado</span>\</span><br><span class="line"></span><br><span class="line"><span class="comment">*-(2)修改系统文件夹的储存位置</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">sysdir</span> <span class="keyword">set</span> PLUS <span class="string">&quot;D:\stata16/ado\plus&quot;</span> </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="2-文件工作路径和设置"><a href="#2-文件工作路径和设置" class="headerlink" title="2.文件工作路径和设置"></a>2.文件工作路径和设置</h3><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">*-(1)显示当前工作路径</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">cd</span> </span><br><span class="line"><span class="keyword">D</span>:\stata_learn\star_stata</span><br><span class="line"></span><br><span class="line"><span class="comment">*-(2)改变当前工作路径</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">cd</span> <span class="string">&quot;D:\stata_learn\star_stata&quot;</span> </span><br><span class="line"><span class="keyword">D</span>:\stata_learn\star_stata</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="3-程序文件-ado-路径的增减"><a href="#3-程序文件-ado-路径的增减" class="headerlink" title="3.程序文件(.ado)路径的增减"></a>3.程序文件(.ado)路径的增减</h3><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">*-(1)增加路径</span></span><br><span class="line"></span><br><span class="line">.  <span class="keyword">adopath</span> + <span class="string">&quot;D:\stata_learn\star_stata&quot;</span> <span class="comment">// 该路径下的 .ado 文件也可以被 Stata 识别</span></span><br><span class="line">  [1]  (BASE)      <span class="string">&quot;D:\stata16\ado\base/&quot;</span></span><br><span class="line">  [2]  (SITE)      <span class="string">&quot;D:\stata16\ado\site/&quot;</span></span><br><span class="line">  [3]  (<span class="keyword">PERSONAL</span>)  <span class="string">&quot;D:\stata16/ado\personal/&quot;</span></span><br><span class="line">  [4]  (PLUS)      <span class="string">&quot;D:\stata16/ado\plus/&quot;</span></span><br><span class="line">  [5]  (OLDPLACE)  <span class="string">&quot;c:\ado/&quot;</span></span><br><span class="line">  [6]              <span class="string">&quot;D:\stata_learn\star_stata&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-(2)减少路径</span></span><br><span class="line"></span><br><span class="line">.  <span class="keyword">adopath</span> - <span class="string">&quot;D:\stata_learn\star_stata&quot;</span> <span class="comment">// 相同道理</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="4-快速创建文件夹与子文件夹"><a href="#4-快速创建文件夹与子文件夹" class="headerlink" title="4.快速创建文件夹与子文件夹"></a>4.快速创建文件夹与子文件夹</h3><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">. efolder zhang2022, <span class="keyword">cd</span>(<span class="keyword">D</span>:\stata_learn\star_stata) sub(data_raw data_new <span class="keyword">out</span> refs logfile ) noc</span><br><span class="line">A subfolder named data_raw created.</span><br><span class="line">A subfolder named data_new created.</span><br><span class="line">A subfolder named <span class="keyword">out</span> created.</span><br><span class="line">A subfolder named refs created.</span><br><span class="line">A subfolder named logfile created.</span><br><span class="line">A folder named zhang2022 created.      [zhang2022] </span><br><span class="line">Working directory has been changed into the main folder just created or the path <span class="keyword">in</span> <span class="keyword">cd</span> option</span><br><span class="line"></span><br><span class="line"><span class="comment">// noc：不改变目前工作路径</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="5-profile-文件的设定"><a href="#5-profile-文件的设定" class="headerlink" title="5.profile 文件的设定"></a>5.profile 文件的设定</h3><ul>
<li><strong>主要是：</strong>1）设置外部命令的储存位置；2）自动生成 log 文件，记录操作过程</li>
<li><strong>详见：</strong><a target="_blank" rel="noopener" href="https://www.lianxh.cn/news/ad2b49ad17a3f.html">连享会——聊聊Stata中的profile文件</a></li>
</ul>
<h3 id="6-do-editor-中的快捷键"><a href="#6-do-editor-中的快捷键" class="headerlink" title="6.do-editor 中的快捷键"></a>6.do-editor 中的快捷键</h3><ul>
<li><strong>Ctrl+D</strong> 		执行(Do)选中的命令 </li>
<li><strong>Ctrl+F</strong>		  在do-editor中搜索特定的关键词</li>
</ul>
<h3 id="7-数字显示格式-基础"><a href="#7-数字显示格式-基础" class="headerlink" title="7.数字显示格式-基础"></a>7.数字显示格式-基础</h3><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">. <span class="keyword">dis</span>  %7.2f  1000.12345</span><br><span class="line">1000.12</span><br><span class="line"></span><br><span class="line">. <span class="keyword">dis</span>  %9.2e  1000.12345</span><br><span class="line"> 1.00e+03</span><br><span class="line"></span><br><span class="line">. <span class="keyword">dis</span>  %8.2fc  1000.12345</span><br><span class="line">1,000.12</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>&#x3D;&#x3D;<font color=#6F00D2> % </font>&#x3D;&#x3D; ：表示开始设置格式</li>
<li>&#x3D;&#x3D;<font color=#6F00D2>7</font>&#x3D;&#x3D; ：表示所占位数，即宽度</li>
<li>&#x3D;&#x3D;<font color=#6F00D2>2</font>&#x3D;&#x3D; ：表示保留几位小数</li>
<li>&#x3D;&#x3D;<font color= #6F00D2>c</font>&#x3D;&#x3D; ：将数据设置成西方计数方法，即小数点之前每三位打一个逗号</li>
<li>&#x3D;&#x3D;<font color= #6F00D2>e</font>&#x3D;&#x3D; ：结果用科学计数法表示</li>
<li>&#x3D;&#x3D;<font color= #6F00D2>f</font>&#x3D;&#x3D; ：固定格式，常用</li>
<li>&#x3D;&#x3D;<font color= #6F00D2>g</font>&#x3D;&#x3D; ：表示通用设定，即Stata会自动设定显示格式</li>
<li>&#x3D;&#x3D;<font color= #6F00D2>help format</font>&#x3D;&#x3D;</li>
<li><strong>详见</strong>   <a target="_blank" rel="noopener" href="https://www.jianshu.com/p/cf49613b4227">简书-如何设定Stata中数字的显示格式</a></li>
</ul>
<h3 id="8-数字显示格式-应用"><a href="#8-数字显示格式-应用" class="headerlink" title="8.数字显示格式-应用"></a>8.数字显示格式-应用</h3><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">*-(1)数值型变量的显示格式</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">format</span> gear %6.3f    <span class="comment">// 宽度为6字符，并保留三位小数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-(2)描述性统计中数字的显示格式</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">tabstat</span> price mpg rep78 length turn foreign, <span class="comment">///</span></span><br><span class="line">      stats(<span class="keyword">n</span> <span class="keyword">mean</span> sd min max) c(s) <span class="built_in">f</span>(%9.2f)</span><br><span class="line"></span><br><span class="line"><span class="comment">*-(3)估计结果中数字的显示格式</span></span><br><span class="line"></span><br><span class="line">. esttab reg1 reg2, b(%9.2f) <span class="keyword">se</span>(%7.2f) sfmt(%7.3f)    <span class="comment">///</span></span><br><span class="line">       star(* 0.1 ** 0.05 *** 0.01) mtitle(reg1 reg2) <span class="comment">///</span></span><br><span class="line">       <span class="built_in">scalar</span>(r2 r2_a <span class="keyword">N</span> F) <span class="keyword">compress</span> nogap</span><br><span class="line"></span><br><span class="line"><span class="comment">*-(4)图片中数字的显示格式</span></span><br><span class="line"></span><br><span class="line">#delimit ;</span><br><span class="line"><span class="keyword">twoway</span> (<span class="keyword">scatter</span> price mpg) (<span class="keyword">lfit</span> price mpg)</span><br><span class="line">    ,</span><br><span class="line">    ylabel(, <span class="keyword">format</span>(%9.1f) angle(0) nogrid)</span><br><span class="line">    xlabel(, <span class="keyword">format</span>(%3.1f))</span><br><span class="line">;</span><br><span class="line">#delimit c</span><br><span class="line"></span><br><span class="line"><span class="comment">*-使用这个语法，中间就不用多打很多的换行符</span></span><br></pre></td></tr></table></figure>

<ul>
<li><strong>详见</strong>   <a target="_blank" rel="noopener" href="https://www.jianshu.com/p/cf49613b4227">简书-如何设定Stata中数字的显示格式</a></li>
</ul>
<h2 id="二、stata-数据处理"><a href="#二、stata-数据处理" class="headerlink" title="二、stata 数据处理"></a>二、stata 数据处理</h2><h3 id="1-数据导入"><a href="#1-数据导入" class="headerlink" title="1.数据导入"></a>1.数据导入</h3><h3 id="2-数据合并"><a href="#2-数据合并" class="headerlink" title="2.数据合并"></a>2.数据合并</h3><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">*-(1)横向合并：增加变量 ---- merge</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-1) 1:1 合并</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">use</span> <span class="string">&quot;A.dta&quot;</span>, <span class="keyword">clear</span></span><br><span class="line">. <span class="keyword">merge</span> 1:1 id using <span class="string">&quot;B.dta&quot;</span>,nogen <span class="comment">// 两份数据均是一维</span></span><br><span class="line">. <span class="keyword">merge</span> 1:1 id year using <span class="string">&quot;B.dta&quot;</span>,nogen <span class="comment">// 两份数据均是二维</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-注：1:1 指两份数据 A.dta 与 B.dta 的维度是相同的</span></span><br><span class="line"><span class="comment">*    如都是截面数据（仅包含id 或 year）或都是面板数据（同时包含id 和 year）</span></span><br><span class="line"><span class="comment">*    nogen 则仅显示 matched 好的样本</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 数据示意</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">     A.dta             | B.dta          | A_B.dta 	  	    </span></span><br><span class="line"><span class="comment">     id  x1  x2  x3    | id  x4  x5     | id x1  x2  x3	 x4  x5         </span></span><br><span class="line"><span class="comment">     1                 | 2              | 2 </span></span><br><span class="line"><span class="comment">     2                 | 3              | 3 </span></span><br><span class="line"><span class="comment">     3                 | 4              |                   */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-2) m:1 合并</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">use</span> <span class="string">&quot;C.dta&quot;</span>, <span class="keyword">clear</span></span><br><span class="line">. <span class="keyword">merge</span> <span class="keyword">m</span>:1 id using <span class="string">&quot;B.dta&quot;</span>,nogen <span class="comment">// 首先判断两份数据的维度</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-注：C.dta 为多维，并以 C.dta 为基础进行合并</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 数据示意</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">     C.dta                | B.dta       | C_B.dta 	  	    </span></span><br><span class="line"><span class="comment">     id  year x1  x2      | id  x3      | id  year  x1  x2  x3	         </span></span><br><span class="line"><span class="comment">     1   2001 1   2       | 1   3       | 1   2001  1   2   4</span></span><br><span class="line"><span class="comment">     1   2002 3   4       | 2   4       | 1   2002  3   4   4</span></span><br><span class="line"><span class="comment">     5   2001 5   6       | 3   5       | 5   2001  5   6   .       */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-(2)纵向合并：追加样本 ---- append</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">use</span> <span class="string">&quot;A.dta&quot;</span>, <span class="keyword">clear</span></span><br><span class="line">. <span class="keyword">append</span> using B.dta</span><br><span class="line"></span><br><span class="line"><span class="comment">*-注：</span></span><br><span class="line"><span class="comment">   *  1) 两个数据库中的变量名称要相同</span></span><br><span class="line"><span class="comment">   *     PRICE 和 price 是不同的变量</span></span><br><span class="line"><span class="comment">   *  2) 两个数据库中的同名变量要具有相同的存储类型</span></span><br><span class="line"><span class="comment">   *     同为文字变量或同为数值变量</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-(3)数据横纵变换</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-1）变量名无字符，纯数字 ---- xpose</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">xpose</span>, <span class="keyword">clear</span>   <span class="comment">// 对调，不能对包含文字变量进行转换，需要重新设置变量名</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-2）变量名有字符 ---- reshape</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">/*  原始资料(可能来自网页等途径)  -wide-型数据</span></span><br><span class="line"><span class="comment">                       income                unemployee      </span></span><br><span class="line"><span class="comment">   -----------------------------------+----------------------</span></span><br><span class="line"><span class="comment">    id   sex    1980    1981    1982  |  1980   1981    1982 </span></span><br><span class="line"><span class="comment">   -----------------------------------+----------------------</span></span><br><span class="line"><span class="comment">     1     0    5000    5500    6000  |    0      1      0   </span></span><br><span class="line"><span class="comment">     2     1    2000    2200    3300  |    1      0      0   </span></span><br><span class="line"><span class="comment">     3     0    3000    2000    1000  |    0      0      1   </span></span><br><span class="line"><span class="comment">   -----------------------------------+----------------------</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">  * 整理后的资料     -wide-型数据</span></span><br><span class="line"><span class="comment">    id   sex   inc80   inc81   inc82   ue80   ue81   ue82 </span></span><br><span class="line"><span class="comment">     1     0    5000    5500    6000      0      1      0 </span></span><br><span class="line"><span class="comment">     2     1    2000    2200    3300      1      0      0 </span></span><br><span class="line"><span class="comment">     3     0    3000    2000    1000      0      0      1 </span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">  * 最终需要的资料(panel data)    -long-型数据</span></span><br><span class="line"><span class="comment">    id   year   sex    inc   ue  </span></span><br><span class="line"><span class="comment">     1   1980     0   5000    0  </span></span><br><span class="line"><span class="comment">     1   1981     0   5500    1  </span></span><br><span class="line"><span class="comment">     1   1982     0   6000    0  </span></span><br><span class="line"><span class="comment">     2   1980     1   2000    1  </span></span><br><span class="line"><span class="comment">     2   1981     1   2200    0  </span></span><br><span class="line"><span class="comment">     2   1982     1   3300    0  </span></span><br><span class="line"><span class="comment">     3   1980     0   3000    0  </span></span><br><span class="line"><span class="comment">     3   1981     0   2000    0  </span></span><br><span class="line"><span class="comment">     3   1982     0   1000    1  </span></span><br><span class="line"><span class="comment">*/</span>     </span><br><span class="line"></span><br><span class="line"><span class="comment"> *- wide --&gt; long</span></span><br><span class="line">     <span class="keyword">use</span> <span class="string">&quot;reshape1.dta&quot;</span>, <span class="keyword">clear</span></span><br><span class="line">     <span class="keyword">reshape</span> long inc ue, <span class="built_in">i</span>(id) <span class="built_in">j</span>(year) <span class="comment">// sex 不发生变化，无需转换</span></span><br><span class="line">                                        <span class="comment">// j() 选项中填写新的变量名称</span></span><br><span class="line">     <span class="keyword">replace</span> year = 1900 + year</span><br><span class="line">    </span><br><span class="line"><span class="comment">  *- long --&gt; wide</span></span><br><span class="line">     <span class="keyword">reshape</span> wide inc ue, <span class="built_in">i</span>(id) <span class="built_in">j</span>(year)</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h3 id="EPS-数据库与-stata"><a href="#EPS-数据库与-stata" class="headerlink" title="EPS 数据库与 stata"></a>EPS 数据库与 stata</h3><h3 id="n-生成差分项、滞后项与前移项"><a href="#n-生成差分项、滞后项与前移项" class="headerlink" title="n.生成差分项、滞后项与前移项"></a>n.生成差分项、滞后项与前移项</h3><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">*-(1)gen 命令</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">gen</span> Dx1 = <span class="keyword">D</span>.x1         <span class="comment">// 一阶差分</span></span><br><span class="line">. <span class="keyword">gen</span> L1x1 = L1.x1       <span class="comment">// 一期滞后项</span></span><br><span class="line">. <span class="keyword">gen</span> L2x1 = L2.x1       <span class="comment">// 两期滞后项</span></span><br><span class="line"></span><br><span class="line"><span class="comment">*-(2)reg 回归</span></span><br><span class="line"></span><br><span class="line">. <span class="keyword">reg</span> x1 <span class="keyword">L</span>(1/3).x1 <span class="built_in">F</span>(1/3).x1 <span class="comment">// 可以不必生成，回归中直接引用即可	</span></span><br><span class="line"><span class="comment">*-生成 x1 的滞后项 L1 L2 L3 与前移项 F1 F2 F3 ，并进行 reg 回归</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>





<p>stata 运算</p>
<p>缺失值的处理</p>
<h2 id="三、stata-画图"><a href="#三、stata-画图" class="headerlink" title="三、stata 画图"></a>三、stata 画图</h2><h2 id="四、stata-回归分析"><a href="#四、stata-回归分析" class="headerlink" title="四、stata 回归分析"></a>四、stata 回归分析</h2>
    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/star8kr/tags/Stata/" rel="tag"># Stata</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/star8kr/2023/04/18/Stata%E5%A6%82%E4%BD%95%E7%94%9F%E6%88%90%E5%A4%A9%E9%A9%AC%E8%A1%8C%E7%A9%BA%E7%9A%84%E7%A9%BA%E9%97%B4%E6%9D%83%E9%87%8D%E7%9F%A9%E9%98%B5%EF%BC%9F/" rel="prev" title="Stata 如何生成天马行空的空间权重矩阵？">
      <i class="fa fa-chevron-left"></i> Stata 如何生成天马行空的空间权重矩阵？
    </a></div>
      <div class="post-nav-item">
    <a href="/star8kr/2023/05/13/%E9%9A%8F%E7%AC%94_%E8%80%81%E7%94%B7%E5%AD%A9/" rel="next" title="流水账|老男孩">
      流水账|老男孩 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E3%80%81stata-%E5%9F%BA%E7%A1%80%E8%AE%BE%E7%BD%AE"><span class="nav-text">一、stata 基础设置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E7%B3%BB%E7%BB%9F%E6%96%87%E4%BB%B6%E4%BD%8D%E7%BD%AE%E6%9F%A5%E8%AF%A2%E4%B8%8E%E4%BF%AE%E6%94%B9"><span class="nav-text">1.系统文件位置查询与修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%96%87%E4%BB%B6%E5%B7%A5%E4%BD%9C%E8%B7%AF%E5%BE%84%E5%92%8C%E8%AE%BE%E7%BD%AE"><span class="nav-text">2.文件工作路径和设置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E7%A8%8B%E5%BA%8F%E6%96%87%E4%BB%B6-ado-%E8%B7%AF%E5%BE%84%E7%9A%84%E5%A2%9E%E5%87%8F"><span class="nav-text">3.程序文件(.ado)路径的增减</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E5%BF%AB%E9%80%9F%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6%E5%A4%B9%E4%B8%8E%E5%AD%90%E6%96%87%E4%BB%B6%E5%A4%B9"><span class="nav-text">4.快速创建文件夹与子文件夹</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-profile-%E6%96%87%E4%BB%B6%E7%9A%84%E8%AE%BE%E5%AE%9A"><span class="nav-text">5.profile 文件的设定</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-do-editor-%E4%B8%AD%E7%9A%84%E5%BF%AB%E6%8D%B7%E9%94%AE"><span class="nav-text">6.do-editor 中的快捷键</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-%E6%95%B0%E5%AD%97%E6%98%BE%E7%A4%BA%E6%A0%BC%E5%BC%8F-%E5%9F%BA%E7%A1%80"><span class="nav-text">7.数字显示格式-基础</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-%E6%95%B0%E5%AD%97%E6%98%BE%E7%A4%BA%E6%A0%BC%E5%BC%8F-%E5%BA%94%E7%94%A8"><span class="nav-text">8.数字显示格式-应用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E3%80%81stata-%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86"><span class="nav-text">二、stata 数据处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%95%B0%E6%8D%AE%E5%AF%BC%E5%85%A5"><span class="nav-text">1.数据导入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%95%B0%E6%8D%AE%E5%90%88%E5%B9%B6"><span class="nav-text">2.数据合并</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#EPS-%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%8E-stata"><span class="nav-text">EPS 数据库与 stata</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#n-%E7%94%9F%E6%88%90%E5%B7%AE%E5%88%86%E9%A1%B9%E3%80%81%E6%BB%9E%E5%90%8E%E9%A1%B9%E4%B8%8E%E5%89%8D%E7%A7%BB%E9%A1%B9"><span class="nav-text">n.生成差分项、滞后项与前移项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89%E3%80%81stata-%E7%94%BB%E5%9B%BE"><span class="nav-text">三、stata 画图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9B%E3%80%81stata-%E5%9B%9E%E5%BD%92%E5%88%86%E6%9E%90"><span class="nav-text">四、stata 回归分析</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">zhang jiaxing</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/star8kr/archives/">
        
          <span class="site-state-item-count">5</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/star8kr/categories/">
          
        <span class="site-state-item-count">2</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/star8kr/tags/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">zhang jiaxing</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">27k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">1:07</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/star8kr/lib/anime.min.js"></script>
  <script src="/star8kr/lib/velocity/velocity.min.js"></script>
  <script src="/star8kr/lib/velocity/velocity.ui.min.js"></script>

<script src="/star8kr/js/utils.js"></script>

<script src="/star8kr/js/motion.js"></script>


<script src="/star8kr/js/schemes/pisces.js"></script>


<script src="/star8kr/js/next-boot.js"></script>




  




  
<script src="/star8kr/js/local-search.js"></script>













  

  

</body>
</html>
